package edu.spec.pms.controller;

import edu.spec.pms.model.dto.MvcReturn;
import edu.spec.pms.model.sys.UserRole;
import edu.spec.pms.service.iface.IRoleService;
import edu.spec.pms.service.iface.IUserRoleService;
import edu.spec.pms.service.iface.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;

@Controller
@RequestMapping("/admin/userRole")
public class UserRoleController {

	@Inject
	private IUserRoleService urService;
	@Inject
	private IUserService userService;
	@Inject
	private IRoleService roleService;

	@ResponseBody
	@RequestMapping("/add")
	public Object add(Integer userId, Integer roleId){
		if(userId==null && roleId==null){
			return new MvcReturn(false, null, "用户或角色不存在");
		}
		UserRole ur = new UserRole();
		ur.setUser(userService.load(userId));
		ur.setRole(roleService.load(roleId));
		urService.add(ur);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/delete")
	public Object deleteByUserAndRole(Integer userId, Integer roleId){
		if(userId==null && roleId==null){
			return new MvcReturn(false, null, "用户或角色不存在");
		}
		urService.deleteByUserAndRole(userId, roleId);
		return new MvcReturn(true);
	}

}
